package com.cjkj.applet.service.impl;

import com.cjkj.applet.domain.AppletAccount;
import com.cjkj.applet.domain.TPatIndex;
import com.cjkj.applet.domain.vo.PatientFormVo;
import com.cjkj.applet.mapper.AppletAccountMapper;
import com.cjkj.applet.mapper.THospitalUserMapper;
import com.cjkj.applet.mapper.TPatIndexMapper;
import com.cjkj.applet.service.ITPatIndexService;
import com.cjkj.common.core.exception.ServiceException;
import com.cjkj.common.core.utils.DateUtils;
import com.cjkj.common.core.utils.StringUtils;
import com.cjkj.common.security.utils.SecurityUtils;
import com.cjkj.system.api.domain.THospitalUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 患者基本信息Service业务层处理
 * 
 * @author cjkj
 * @date 2024-08-30
 */
@Service
public class TPatIndexServiceImpl implements ITPatIndexService 
{
    @Resource
    private TPatIndexMapper tPatIndexMapper;

    @Resource
    private AppletAccountMapper appletAccountMapper;

    @Resource
    private THospitalUserMapper hospitalUserMapper;

    @Override
    public TPatIndex getPatByIdentityNumber(String identityNumber) {
        return tPatIndexMapper.selectPatByIdentityNumber(identityNumber);
    }

    @Override
    public PatientFormVo selectPatById(String identityNumber) {
        // 1、查询小程序账户信息
        AppletAccount account = appletAccountMapper.selectAppletAccountByOpenId(SecurityUtils.getAppOpenId());
        if(account == null) {
            throw new ServiceException("账户不存在！");
        }
        String doctor = account.getDoctor();
        if(StringUtils.isBlank(doctor)) {
            // 如果没有绑定医生账号，直接返回
            throw new ServiceException("您的账户当前暂未绑定医生账号！");
        }

        // 2、查询医生信息
        THospitalUser hospitalUser = hospitalUserMapper.selectTHospitalUserByUserName(doctor);
        if(hospitalUser == null) {
            throw new ServiceException("医生账号不存在！");
        }

        // 3、查询患者信息并返回

        TPatIndex patIndex = tPatIndexMapper.selectByIdNoAndHospitalId(identityNumber, hospitalUser.getHospitalId());
        if(patIndex == null) {
            throw new ServiceException("未在该医院查询到患者相关信息！");
        }
        Date birthday = patIndex.getBirthday();

        PatientFormVo patientFormVo = new PatientFormVo();
        patientFormVo.setSfzNo(patIndex.getIdentityNumber());
        patientFormVo.setCs(birthday);
        patientFormVo.setXb(patIndex.getGender());
        patientFormVo.setXm(patIndex.getPatientName());
        patientFormVo.setNl(DateUtils.getAge(birthday));
        patientFormVo.setPhone((patIndex.getPhone()));
        return patientFormVo;
    }

    /**
     * 查询患者基本信息
     * 
     * @param patientId 患者基本信息主键
     * @return 患者基本信息
     */
    @Override
    public TPatIndex selectTPatIndexByPatientId(Long patientId)
    {
        return tPatIndexMapper.selectTPatIndexByPatientId(patientId);
    }

    /**
     * 查询患者基本信息列表
     * 
     * @param tPatIndex 患者基本信息
     * @return 患者基本信息
     */
    @Override
    public List<TPatIndex> selectTPatIndexList(TPatIndex tPatIndex)
    {
        return tPatIndexMapper.selectTPatIndexList(tPatIndex);
    }

    /**
     * 新增患者基本信息
     * 
     * @param tPatIndex 患者基本信息
     * @return 结果
     */
    @Override
    public int insertTPatIndex(TPatIndex tPatIndex)
    {
        tPatIndex.setCreateTime(DateUtils.getNowDate());
        return tPatIndexMapper.insertTPatIndex(tPatIndex);
    }

    /**
     * 修改患者基本信息
     * 
     * @param tPatIndex 患者基本信息
     * @return 结果
     */
    @Override
    public int updateTPatIndex(TPatIndex tPatIndex)
    {
        tPatIndex.setUpdateTime(DateUtils.getNowDate());
        return tPatIndexMapper.updateTPatIndex(tPatIndex);
    }

    /**
     * 批量删除患者基本信息
     * 
     * @param patientIds 需要删除的患者基本信息主键
     * @return 结果
     */
    @Override
    public int deleteTPatIndexByPatientIds(Long[] patientIds)
    {
        return tPatIndexMapper.deleteTPatIndexByPatientIds(patientIds);
    }

    /**
     * 删除患者基本信息信息
     * 
     * @param patientId 患者基本信息主键
     * @return 结果
     */
    @Override
    public int deleteTPatIndexByPatientId(Long patientId)
    {
        return tPatIndexMapper.deleteTPatIndexByPatientId(patientId);
    }
}
